package com.ruoyi.puyu.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.puyu.domain.PuyuCoach;
import com.ruoyi.puyu.domain.PuyuLesson;
import com.ruoyi.puyu.mapper.PuyuCoachMapper;
import com.ruoyi.puyu.mapper.PuyuLessonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.puyu.mapper.PuyuCoachSalaryMapper;
import com.ruoyi.puyu.domain.PuyuCoachSalary;
import com.ruoyi.puyu.service.IPuyuCoachSalaryService;

/**
 * 教练薪资Service业务层处理
 * 
 * @author wusongwen
 * @date 2024-05-01
 */
@Service
public class PuyuCoachSalaryServiceImpl implements IPuyuCoachSalaryService 
{
    private static final Logger logger = LoggerFactory.getLogger(PuyuCoachSalaryServiceImpl.class);
    @Autowired
    private PuyuCoachSalaryMapper puyuCoachSalaryMapper;
    @Autowired
    private PuyuLessonMapper puyuLessonMapper;
    @Autowired
    private PuyuCoachMapper puyuCoachMapper;

    /**
     * 查询教练薪资
     * 
     * @param coachSalaryId 教练薪资主键
     * @return 教练薪资
     */
    @Override
    public PuyuCoachSalary selectPuyuCoachSalaryByCoachSalaryId(Long coachSalaryId)
    {
        return puyuCoachSalaryMapper.selectPuyuCoachSalaryByCoachSalaryId(coachSalaryId);
    }

    /**
     * 查询教练薪资列表
     * 
     * @param puyuCoachSalary 教练薪资
     * @return 教练薪资
     */
    @Override
    public List<PuyuCoachSalary> selectPuyuCoachSalaryList(PuyuCoachSalary puyuCoachSalary)
    {
        return puyuCoachSalaryMapper.selectPuyuCoachSalaryList(puyuCoachSalary);
    }

    /**
     * 新增教练薪资
     * 
     * @param puyuCoachSalary 教练薪资
     * @return 结果
     */
    @Override
    public int insertPuyuCoachSalary(PuyuCoachSalary puyuCoachSalary)
    {
        // 获取教练数据
        PuyuCoach puyuCoach = puyuCoachMapper.selectPuyuCoachByCoachId(puyuCoachSalary.getCoachId());
        if (null == puyuCoach) {
            logger.info("教练不存在，教练编号：{}", puyuCoachSalary.getCoachId());
            throw new ServiceException(MessageUtils.message("coach.not.exists"));
        }
        // 验证教练工资记录是否存在
        PuyuCoachSalary params = new PuyuCoachSalary();
        params.setCoachId(puyuCoachSalary.getCoachId());
        params.setSalaryMonth(puyuCoachSalary.getSalaryMonth());
        List<PuyuCoachSalary> list = puyuCoachSalaryMapper.selectPuyuCoachSalaryList(params);

        if (null != list && !list.isEmpty()) {
            logger.info("教练工资记录已存在，教练编号：{}, 月份: {}", puyuCoachSalary.getCoachId(), puyuCoachSalary.getSalaryMonth());
            throw new ServiceException(MessageUtils.message("coach.salary.exists"));
        }

        puyuCoachSalary.setSalaryBasic(puyuCoach.getSalaryBasic());
        puyuCoachSalary.setClassFee(puyuCoach.getClassFee());

        // 获取课时计算起始时间
        DateTime beginTime = DateUtil.parse(puyuCoachSalary.getSalaryMonth() + "01", DatePattern.PURE_DATE_PATTERN);
        // 获取课时计算截止时间
        DateTime endTime = DateUtil.endOfMonth(beginTime);
        PuyuLesson puyuLesson = new PuyuLesson();
        puyuLesson.setCoachId(puyuCoachSalary.getCoachId());
        puyuLesson.getParams().put("beginTime", beginTime);
        puyuLesson.getParams().put("endTime", endTime);
        // 获取教练课时
        int lessonNum = puyuLessonMapper.selectPuyuLessonCountByCoachId(puyuLesson);
        puyuCoachSalary.setLessonNum(lessonNum);
        // 合计
        BigDecimal salarySum = puyuCoachSalary.getSalaryBasic().add(puyuCoachSalary.getClassFee().multiply(new BigDecimal(lessonNum))).add(puyuCoachSalary.getSubsidy());
        puyuCoachSalary.setSalarySum(salarySum);

        puyuCoachSalary.setCreateBy(SecurityUtils.getUsername());
        puyuCoachSalary.setCreateTime(DateUtils.getNowDate());
        return puyuCoachSalaryMapper.insertPuyuCoachSalary(puyuCoachSalary);
    }

    /**
     * 修改教练薪资
     * 
     * @param puyuCoachSalary 教练薪资
     * @return 结果
     */
    @Override
    public int updatePuyuCoachSalary(PuyuCoachSalary puyuCoachSalary)
    {
        puyuCoachSalary.setUpdateTime(DateUtils.getNowDate());
        return puyuCoachSalaryMapper.updatePuyuCoachSalary(puyuCoachSalary);
    }

    /**
     * 批量删除教练薪资
     * 
     * @param coachSalaryIds 需要删除的教练薪资主键
     * @return 结果
     */
    @Override
    public int deletePuyuCoachSalaryByCoachSalaryIds(Long[] coachSalaryIds)
    {
        return puyuCoachSalaryMapper.updatePuyuCoachSalaryByCoachSalaryIds(coachSalaryIds);
    }

    /**
     * 删除教练薪资信息
     * 
     * @param coachSalaryId 教练薪资主键
     * @return 结果
     */
    @Override
    public int deletePuyuCoachSalaryByCoachSalaryId(Long coachSalaryId)
    {
        return puyuCoachSalaryMapper.deletePuyuCoachSalaryByCoachSalaryId(coachSalaryId);
    }
}
